package pro.softzhang.algo.lc300;

import pro.softzhang.algo.flag.difficulty.DYellow;

import java.util.LinkedList;
import java.util.Queue;

@DYellow
public class LC346_MovingAverageFromDataStream {
    public static void main(String[] args) {
        MovingAverage ma = new MovingAverage(3);
        System.out.println(ma.next(1));
        System.out.println(ma.next(10));
        System.out.println(ma.next(3));
        System.out.println(ma.next(5));
    }

    static class MovingAverage {
        private Queue<Integer> q;
        private int capacity;
        private int sum = 0;

        public MovingAverage(int size) {
            q = new LinkedList<>();
            capacity = size;
        }

        public double next(int val) {
            q.offer(val);
            sum += val;
            if(q.size() > capacity) {
                sum -= q.poll();
            }
            return sum * 1.0 / q.size();
        }
    }

}